package com.ag.zhh.action;

import javax.annotation.Resource;

import com.ag.bean.TbPlan;
import com.ag.common.BaseAction;
import com.ag.common.EventUtil;
import com.ag.lwc.service.impl.LogService;
import com.ag.zhh.service.CurrentEventService;
import com.ag.zhh.service.EventPlanService;

/**
 * 进入处置流程的ACTION （1.启动预案 2. 采取应急措施）
 * 
 * @author 张颢铧
 * 
 */
public class ProcessStartAction extends BaseAction {

	/**
	 * 进入采取紧急措施的应急流程
	 * 
	 * @return
	 */
	public String processEventStart() {
		if(checkEventFinished()==true){
			this.setResult("faile");
			return "processEventStart";
		}
		processEvent();
		System.out.println("进入应急处置流程：采取紧急措施，事件ID——"
				+ getSession().getAttribute(EventUtil.PROCESSING_EVENT_ID));
		this.setResult("success");
		return "processEventStart";
	}

	/**
	 * 进入启动预案的应急流程
	 * 
	 * @return
	 */
	public String processEventStartByPlan() {
		if(checkEventFinished()==true){
			this.setResult("faile");
			return "processEventStartByPlan";
		}
		TbPlan tbPlan = eventPlanService.getStartedTbPlanByEId(eventId);
		if(tbPlan == null) {
			throw new RuntimeException("ProcessStartAction:processEventStartByPlan() ----事件还未启动预案！");
		}
		this.setPlanId(tbPlan.getPId());
		processEventByPlan();
		System.out.println("进入应急处置流程：启动预案，事件ID——"
				+ getSession().getAttribute(EventUtil.PROCESSING_EVENT_ID));
		System.out.println("进入应急处置流程：启动预案，预案ID——"
				+ getSession().getAttribute(EventUtil.PROCESSING_PLAN_ID));
		this.setResult("success");
		return "processEventStartByPlan";
	}

	

	/**
	 * 添加预案处置事件，并更改事件状态
	 */
	private void addCurrentEvent() {
		currentEventService.processByCurrentEvent(eventId);
	}
	
	private boolean checkEventFinished(){
		return currentEventService.checkEventStatusFinish(eventId);
	}
	private void processEvent() {
		getSession().setAttribute(EventUtil.PROCESSING_EVENT_ID, eventId);
		addCurrentEvent();
	}

	private void processEventByPlan() {
		getSession().setAttribute(EventUtil.PROCESSING_EVENT_ID, eventId);
		getSession().setAttribute(EventUtil.PROCESSING_PLAN_ID, planId);
		addCurrentEvent();
	}
	private CurrentEventService currentEventService;
	
	
	private EventPlanService eventPlanService;
	


	private String eventId;

	private String planId;

	private String result;

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public String getEventId() {
		return eventId;
	}

	public void setEventId(String eventId) {
		this.eventId = eventId;
	}

	public String getPlanId() {
		return planId;
	}

	public void setPlanId(String planId) {
		this.planId = planId;
	}

	public ProcessStartAction() {
		super();
	}

	public CurrentEventService getCurrentEventService() {
		return currentEventService;
	}

	public void setCurrentEventService(CurrentEventService currentEventService) {
		this.currentEventService = currentEventService;
	}

	public EventPlanService getEventPlanService() {
		return eventPlanService;
	}

	public void setEventPlanService(EventPlanService eventPlanService) {
		this.eventPlanService = eventPlanService;
	}
}
